package echat.future.cn.echat.my.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.io.File;
import java.io.FileOutputStream;

import echat.future.cn.echat.R;
import jp.wasabeef.glide.transformations.BlurTransformation;

public class ImageLoadUtils {

    public static void loadBlurImage(Context context, String imgeUrl, ImageView iv){
        //加载背景，
        Glide.with(context)
                .load(imgeUrl)
                .apply(RequestOptions.
                        bitmapTransform(new BlurTransformation(25,3))
                .error(R.color.helftranslucent_background)).
                into(iv);
    }

    public static void loadImage(Context context, String imgeUrl, ImageView iv){
        //加载背景，
        RequestOptions options = new RequestOptions().error(R.mipmap.ic_launcher)
                .dontAnimate();
        Glide
                .with(context)
                .load(imgeUrl)
                .apply(options)
                .into(iv);
    }


    public static File saveBitMapToFile(Context context, String fileName, Bitmap bitmap, boolean isCover) {
        if(null == context || null == bitmap) {
            return null;
        }
        if(TextUtils.isEmpty(fileName)) {
            return null;
        }
        FileOutputStream fOut = null;
        try {
            File file = null;
            String fileDstPath = "";
            if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
                // 保存到sd卡
                fileDstPath = Environment.getExternalStorageDirectory().getAbsolutePath()
                        + File.separator + "phonelive_bitmap" + File.separator + fileName;

                File homeDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
                        + File.separator + "phonelive_bitmap" + File.separator);
                if (!homeDir.exists()) {
                    homeDir.mkdirs();
                }
            } else {
                // 保存到file目录
                fileDstPath = context.getFilesDir().getAbsolutePath()
                        + File.separator + "phonelive_bitmap" + File.separator + fileName;

                File homeDir = new File(context.getFilesDir().getAbsolutePath()
                        + File.separator + "phonelive_bitmap" + File.separator);
                if (!homeDir.exists()) {
                    homeDir.mkdir();
                }
            }

            file = new File(fileDstPath);

            if (!file.exists() || isCover) {
                // 简单起见，先删除老文件，不管它是否存在。
                file.delete();

                fOut = new FileOutputStream(file);
                if (fileName.endsWith(".jpg")) {
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 75, fOut);
                } else {
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                }
                fOut.flush();
                bitmap.recycle();
            }

            return file;
        } catch (Exception e) {
            return null;
        } finally {
            if(null != fOut) {
                try {
                    fOut.close();
                } catch (Exception e) {
                }
            }
        }
    }
}
